/**
 * Created by liubin on 2017/6/2.
 */


var DtjTableLayer = GameCCSBase.extend({

    mDtjTableUILayer : null,

    mDtjTableSettingLayer : null,

    mKwxApplyTableLayer : null,

    mDtjTableRoomEndLayer : null,

    mDtjTableReplayLayer : null,

    mPlayGameCount : null,

    //游戏设置
    mGameConfig : null,

    //游戏密码
    mTableEnterPwd : null,

    //包房名称
    mGroupName : null,

    //房间号
    Text_roomInfo : null,

    mDtjCardLayer : null,

    mOpenCardIndx : null,


    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/DtjTableLayer.json");
        mo.dtjTable = this;
        this.mPlayGameCount = 0;

        this.Text_roomInfo = mo.seekWidgetByName(this.ccsNode, "Text_roomId");

        this.loadUI();
    },


    onEnter : function(){
        this._super();
        mo.soundHelper.stopMusic();
        mo.eventHelper.add("DtjForceExitTableResponsedtjOK", this, function () {
            if (mo.dtjTableStatus.mTableStatus.LeftPlayTimes == mo.dtjTable.mGameConfig.maxpan){
                mo.showMsgDialogOk("退出牌局成功", function(){
                    this.quitDtjGame();
                }, this);
            }
        });

        mo.eventHelper.add("CancleJuRequestdtj", this, function(cancleJu){
            this.mKwxApplyTableLayer.updateCancelJuInfo(cancleJu);
        });

    },

    onExit : function () {
        this.unschedule(this.refreshLeftTime);
        mo.eventHelper.remove(this);
        this._super();
    },

    reset : function () {
        this.mOpenCardIndx = 0;
        this.mDtjCardLayer.reset();
        this.mDtjTableUILayer.reset();
    },

    loadUI : function () {

        cc.spriteFrameCache.addSpriteFrames("res/dtjCardType.plist", "res/dtjCardType.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis.plist", "res/emojis.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis_index.plist", "res/emojis_index.png");
        cc.spriteFrameCache.addSpriteFrames("res/dtjCards.plist", "res/dtjCards.png");

        //游戏牌层
        this.mDtjCardLayer = new DtjCardLayer();
        this.addChild(this.mDtjCardLayer);

        //ui
        this.mDtjTableUILayer = new DtjTableUILayer();
        this.addChild(this.mDtjTableUILayer);

        //取消
        this.mKwxApplyTableLayer = new KwxApplyTableLayer("dtj");
        this.addChild(this.mKwxApplyTableLayer);

        this.mDtjTableReplayLayer = new DtjTableReplayLayer();
        this.addChild(this.mDtjTableReplayLayer);

        this.mDtjCardLayer.initCardTypeTips();
    },


    loadPlayCard : function () {
        this.mPlayGameCount = 0;
        this.mPlayerType = 1;

        //隐藏录像界面
        this.mDtjTableReplayLayer.setVisible(false);

        //隐藏录像时候的ui
        this.mDtjTableUILayer.setIsReplay(false);
    },

    //播放录像
    loadReplay : function(rePlaydtj, replayId){
        this.mPlayGameCount = 0;
        this.mPlayerType = 0;

        this.mDtjTableUILayer.panel_ready.setVisible(false);

        //录像规则
        mo.dtjTable.setDtjConfig(rePlaydtj.Result.CfgJson, "", "");

        //打开录像界面 并且播放
        this.mDtjTableReplayLayer.setVisible(true);
        this.mDtjTableReplayLayer.setReplayId(replayId);
        this.mDtjTableReplayLayer.onStart();

        //隐藏录像时候的ui
        this.mDtjTableUILayer.setIsReplay(true);

        this.mReplayId = replayId;
        mo.dtjTableStatus.setReplayMsg(rePlaydtj);
        mo.dtjTableStatus.playReplay();
    },

    setDtjConfig : function(Cfgjson, tablePassWord, GroupName){
        this.mGroupName = GroupName;
        this.mTableEnterPwd = tablePassWord;
        this.mGameConfig = JSON.parse(Cfgjson);

        mo.dtjCardHelper.setCardTypeGroup(this.mGameConfig.ruleid);

        //密码
        if (mo.gameHelper.developerVersion == "3.0"){

            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_textRoomId"), "密码：");
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_roomId"), "" + tablePassWord);
        }
    },

    //显示代开房人
    setShowRoomCreate : function(user){
        if (user != null){
            var kwxDaikaifangWidget = new KwxDaikaifangWidget();
            kwxDaikaifangWidget.setUserInfo(user);
            this.addChild(kwxDaikaifangWidget);
            kwxDaikaifangWidget.setPosition(cc.p(105, mo.gameSize.y - 10));
        }
    },

    quitDtjGame : function(){
        if (mo.gameHelper.developerVersion == "2.0"){
            mo.sceneHelper.runSceneHall();
        }else if (mo.gameHelper.developerVersion == "3.0"){
            mo.sceneHelper.exitGameLayer();
            if (this.mGroupId != null){
                mo.hallLayer.intoGroup(this.mGroupId);
            }
        }
    },

    updateTableStep : function (step) {

        if(mo.dtjTableStatus.mTableStatus.Step != 0 && mo.dtjTableStatus.lastStep == 0){
            this.reset();
            var UsedCards = mo.dtjTableStatus.mTableStatus.UsedCards;
            if(!!UsedCards){
                this.mDtjCardLayer.setUsedCards(UsedCards);
            }
        }
        mo.dtjTableStatus.lastStep = mo.dtjTableStatus.mTableStatus.Step;

        //准备界面
        this.mDtjTableUILayer.showReady(step == 0);

        this.mDtjTableUILayer.setGuoState();

        if(step != 1){
            this.mDtjTableUILayer.setQieGuoOrXuguoState(false);
        }

        if (mo.gameHelper.developerVersion == "2.0"){

            var result = "房间号" + mo.dtjTableStatus.mTableStatus.TableID;

            if(this.mGameConfig.guo == 1){
                result += " 有锅 ";
                // result += (this.mGameConfig.xuguo == 0 ? "不" : "可") + "续锅 ";
                result += "锅底" + this.mGameConfig.guomax + "分 ";
            }else{
                result += " 无锅 ";
                var zhuang = ["轮庄", "抢庄", "霸王庄"];
                result += zhuang[this.mGameConfig.hosttype] + " ";
                if (this.mGameConfig.addratetime == 0){
                    result += "每次选分 ";
                }else{
                    result += "固定" + this.mGameConfig.basepoint + "分 ";
                }
            }

            var paijiu = ["大牌九", "小牌九"];
            result += paijiu[this.mGameConfig.paijiu] + " ";

            result += (this.mGameConfig.ruleid == 1 || this.mGameConfig.ruleid == 3) ? "有鬼子 " : "无鬼子 ";
            result += (this.mGameConfig.ruleid == 1 || this.mGameConfig.ruleid == 4) ? "有天九王 " : "无天九王 ";

            mo.gameHelper.setTextString(this.Text_roomInfo, result);
        }

        this.updateLastPlayTimes();
    },

    fapai : function (cardNums, firstSeat, sz) {
        var delayTime = 4.0;
        if(this.mPlayerType  == 0){
            this.mDtjCardLayer.fapai(cardNums, firstSeat);
        }else{
            this.runAction(cc.sequence(
                cc.callFunc(function () {
                    delayTime = this.mDtjCardLayer.yaoSz([parseInt(sz / 10), sz % 10], firstSeat);
                }, this),
                cc.delayTime(delayTime),
                cc.callFunc(function () {
                    this.mDtjTableUILayer.startTimeLeft();
                    this.mDtjCardLayer.fapai(cardNums, firstSeat);
                }, this)
            ));
        }
    },

    setKaipaiSeat : function(viewSeat, cardNums) {
        this.mDtjCardLayer.openOtherCard(viewSeat, cardNums);
    },


    openAllPlayerCards : function (handCards, scores, hosetSeat) {
        if(this.mGameConfig.paijiu == 1){
            this.runAction(cc.sequence(
                cc.callFunc(function(){
                    this.mDtjTableUILayer.setScoreAnim(scores, hosetSeat);
                }, this)
            ));
            return 0;
        }

        var delayTime = 5;

        this.runAction(cc.sequence(
            cc.delayTime(1.0),
            cc.callFunc(function(){
                this.mDtjCardLayer.openOtherCardStepTwo(handCards);
            }, this),
            cc.delayTime(1.0),
            cc.callFunc(function(){
                this.mDtjTableUILayer.setScoreAnim(scores, hosetSeat);
            }, this)
        ));

        return delayTime;

    },


    //大结算
    onShowJuEndedLayer : function(juEndedEventgsbdx){
        //判断是否需要直接退出 如果剩余局数和设置里面的局数一致 就直接退出
        var ruleJson = JSON.parse(juEndedEventgsbdx.CfgJson);

        if (mo.dtjTableStatus.mTableStatus.LeftPlayTimes == ruleJson.maxpan){
            mo.showMsgDialogOk("牌局已经解散", function(){
                this.quitDtjGame();
            }, this);
        }else{
            this.mDtjTableRoomEndLayer = new DtjTableRoomEnd();
            mo.sceneHelper.showEndInfoLayer(this.mDtjTableRoomEndLayer);
            // this.addChild(this.mDtjTableRoomEndLayer);
            this.mDtjTableRoomEndLayer.setRoomEndInfo(juEndedEventgsbdx);

            this.mDtjTableRoomEndLayer.setVisible(true);

            // if (juEndedEventgsbdx.EndFlag != 1){
            //     if(mo.dtjTable.mGameConfig.guo == 1){
            //         if(mo.dtjTableStatus.mTableStatus.LastAction.code == 3 || mo.dtjTableStatus.mTableStatus.LastAction.code == 4){
            //             this.mDtjTableRoomEndLayer.setVisible(true);
            //         }else{
            //             this.mDtjTableRoomEndLayer.setVisible(false);
            //         }
            //     }else{
            //         //自然结束 通过当局结算打开
            //         this.mDtjTableRoomEndLayer.setVisible(false);
            //     }
            // }else{
            //     this.mDtjTableRoomEndLayer.setVisible(true);
            // }
        }
    },


    //更新剩余牌局
    updateLastPlayTimes : function(){
        var time = mo.dtjTableStatus.mTableStatus.LeftPlayTimes;
        if (time != null){
            if (time < 0){
                time = 0;
            }

            var result = this.Text_roomInfo.getString();

            if(this.mGameConfig.guo == 0){
                result += "剩"+time + "局";
            }

            mo.gameHelper.setTextString(this.Text_roomInfo, result);
        }
    },


    //牌桌闪动
    onPlayTableOpAnim : function(){
        this.runAction(cc.sequence(
            cc.delayTime(0.50),
            cc.moveTo(0.05, cc.p(0, -15)),
            cc.moveTo(0.05, cc.p(0, 0)),
            cc.moveTo(0.03, cc.p(0, -10)),
            cc.moveTo(0.03, cc.p(0, 0))
        ));
    },


    //邀请玩家
    onInvite : function(){
        var gameName = "dtj";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，就等你来了(点击加入)";
        var tableId = mo.dtjTableStatus.mTableStatus.TableID;

        var title = "桌号:[" + tableId + "]" + "-" + mo.gameHelper.projectName +"-拼天九";
        var description = configSummary;
        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(1, tableId));
    },

    //分享录像
    onShareReplay : function(){
        var gameName = "dtj";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，在游戏中输入录像id就能看到我的精彩回放了";

        var title = "录像id:" + this.mReplayId.substr(7) + "-" + mo.gameHelper.projectName + "-拼天九";
        var description = configSummary;

        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, this.mReplayId.substr(7)));
    }
});